#!/bin/bash

PART_FILE="Site/Pages/Partitions.txt"

function extractField()
{
	FIELD=$1
	FILE=$2
	echo `grep $FIELD:.* $FILE | sed "s/$FIELD:\(.*\)$/\1/"|grep -v "Music by"|tr '\r' ' '`
}

ABC=`find jazz-abc-parts -name *.txt | grep -v \.hg | sort`

rm -rf Site/Partitions
rm -rf $PART_FILE

mkdir Site/Partitions

pushd Site/Partitions > /dev/null



echo "= Une liste de Partition que je met à disposition" >> ../../$PART_FILE
echo >> ../../$PART_FILE
echo >> ../../$PART_FILE
echo "[cols=\"2,2,5\"]">> ../../$PART_FILE
echo "|===========">> ../../$PART_FILE
echo "|*Titre*|*Compositeur*|*Remarque*">> ../../$PART_FILE

for file in $ABC
do
	title=`extractField 'T' ../../$file`
	compositor=`extractField 'C' ../../$file`
	remark=`extractField 'N' ../../$file`
	
	pdf=`basename $file .txt`".pdf"
	
	echo $pdf
	
	echo "| link:../Partitions/$pdf["$title"] |"$compositor"|"$remark >> ../../$PART_FILE
		
	../../jazz-abc-parts/build.sh ../../$file > /dev/null
done
echo "|===========">> ../../$PART_FILE

popd > /dev/null

